# -*- coding: UTF-8 -*-
"""
@author:zhangxing
@file:standardize_field_utils.py
@time:2021/02
"""
import scrapy
from spidertools.utils.time_utils import get_current_date

from commonresources.inner_utils.standardize_field_utils import check_city_field
from commonresources.spider_items.hebei.items import HeBeiShengGongGongZiYuanJiaoYiPingTaiItem
from commonresources.spiders.basespider import BaseSpider


class HeBeiShengGongGongZiYuanJiaoYiPingTaiSpider(BaseSpider):
    """
        河北省公共资源交易平台     http://ggzy.hebei.gov.cn/hbggfwpt
            详情页：http://ggzy.hebei.gov.cn/hbggfwpt/jydt/salesPlat.html
    """
    name = 'HeBeiShengGongGongZiYuanJiaoYiPingTai'
    name_zh = "河北省公共资源交易平台"
    province = "河北"

    start_urls = ['http://ggzy.hebei.gov.cn/hbggfwpt/jydt/salesPlat.html']

    def __init__(self, full_dose=False):
        super(HeBeiShengGongGongZiYuanJiaoYiPingTaiSpider, self).__init__(full_dose)

    def fake_headers(self):
        return {
            "Host": "ggzy.hebei.gov.cn",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "X-Requested-With": "XMLHttpRequest",
            # "Content-Length": "455",
            "Origin": "http://ggzy.hebei.gov.cn",
            "Connection": "keep-alive",
            "Referer": "http://ggzy.hebei.gov.cn/hbggfwpt/jydt/salesPlat.html",
            "Cookie": "JSESSIONID=FDADCC976DFAD9C70E024249EE5A6886; oauthClientId=demoClient; oauthPath=http://127.0.0.1:8000/EpointWebBuilderZw; oauthLoginUrl=http://127.0.0.1:8000/EpointWebBuilderZw/rest/oauth2/authorize?client_id=demoClient&state=a&response_type=code&scope=user&redirect_uri=; oauthLogoutUrl=http://127.0.0.1:8000/EpointWebBuilderZw/rest/oauth2/logout?redirect_uri=; noOauthRefreshToken=719d81f71e7b2a7e7a67d08e1757f061; noOauthAccessToken=fd0ea75de4db1067e97d6870ed65303e",

        }

    def fake_data(self, pn):
        body = '{"token":"","pn":' + str(
            pn) + ',"rn":10,"sdt":"","edt":"","wd":" ","inc_wd":"","exc_wd":"","fields":"title",' \
                  '"cnum":"001","sort":"{\\"webdate\\":0}","ssort":"title","cl":200,"terminal":"",' \
                  '"condition":[{"fieldName":"categorynum","equal":"003001002","notEqual":null,"equalList":null,' \
                  '"notEqualList":null,"isLike":true,"likeType":2}],"time":null,"highlights":"title","statistics":null,' \
                  '"unionCondition":null,"accuracy":"","noParticiple":"0","searchRange":null,"isBusiness":"1"}'
        return body

    def fake_data_more_than_49(self, page):
        value = "{\"siteGuid\":\"7eb5f7f1-9041-43ad-8e13-8fcb82ea831a\",\"categoryNum\":\"003001002\",\"keyword\":\"\",\"pageIndex\":" + f"{page}" + ",\"pageSize\":10}"
        return {"params": value}

    def fake_headers_more_than_49(self):
        return {"Host": "ggzy.hebei.gov.cn",
                "Connection": "keep-alive",
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "Origin": "http://ggzy.hebei.gov.cn",
                "X-Requested-With": "XMLHttpRequest",
                "Authorization": "Bearer 5515a97a21ef3be3b552aaf91f1f2cf3",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400",
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "Referer": "http://ggzy.hebei.gov.cn/hbggfwpt/jydt/salesPlat.html",
                "Accept-Encoding": "gzip, deflate",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Cookie": "oauthClientId=demoClient; oauthPath=http://127.0.0.1:8000/EpointWebBuilderZw; oauthLoginUrl=http://127.0.0.1:8000/EpointWebBuilderZw/rest/oauth2/authorize?client_id=demoClient&state=a&response_type=code&scope=user&redirect_uri=; oauthLogoutUrl=http://127.0.0.1:8000/EpointWebBuilderZw/rest/oauth2/logout?redirect_uri=; noOauthRefreshToken=10a954780753552f4a6120e815499063; noOauthAccessToken=5515a97a21ef3be3b552aaf91f1f2cf3"}

    def start_requests(self):
        yield scrapy.FormRequest(
            url="http://ggzy.hebei.gov.cn/inteligentsearch/rest/inteligentSearch/getFullTextData",
            method="POST",
            headers=self.fake_headers(),
            body=self.fake_data(0),
            meta={
                "need_break": False,
                "page": 1,
                "page_count": -1,
            },
            callback=self.handle_response,
        )

    def handle_response(self, response):
        if response.meta['page'] >= 50:  # 50页及50页以后
            flag = 0
            response_obj = response.json()["Table"]
        else:  # 前49页
            flag = 1
            response_obj = response.json()["result"]["records"]
        if response.meta["page_count"] == -1:
            response.meta["page_count"] = int(int(response.json()["result"]['categorys'][0]['count']) / 10) + 1
        for obj in response_obj:
            item = dict()
            item['release_time'] = obj['infodate'][:10]
            if not self.full_dose and item['release_time'] != get_current_date():
                response.request.meta['need_break'] = True
            elif item['release_time'] < "2015-12-31":
                response.meta['need_break'] = True
            else:
                item['item'] = HeBeiShengGongGongZiYuanJiaoYiPingTaiItem()
                item['announcement_title'] = obj['title']
                item['announcement_type'] = obj['categoryname']
                part_url = obj['linkurl'] if flag else obj['infourl']
                item['origin_url'] = "http://ggzy.hebei.gov.cn/hbggfwpt" + part_url
                check_city_field_ = check_city_field(obj['infod'])
                if check_city_field_:
                    item['project_city'] = check_city_field_
                elif obj['infod'] == "省本级":
                    pass
                else:
                    item['project_area'] = obj['infod']
                yield scrapy.Request(url=item['origin_url'],
                                     # headers=self.faker_headers(),
                                     callback=self.parse_item_new,
                                     dont_filter=True,
                                     meta=item)

        if not response.meta['need_break']:
            page = response.meta['page']
            page_count = response.meta['page_count']
            if page < page_count:
                page += 1
                if page <= 49:
                    yield scrapy.FormRequest(
                        url="http://ggzy.hebei.gov.cn/inteligentsearch/rest/inteligentSearch/getFullTextData",
                        method="POST",
                        headers=self.fake_headers(),
                        body=self.fake_data((page - 1) * 10),
                        meta={
                            "need_break": False,
                            "page": page,
                            "page_count": page_count,
                        },
                        callback=self.handle_response,
                        dont_filter=True,
                    )
                else:
                    yield scrapy.FormRequest(
                        url='http://ggzy.hebei.gov.cn/EWB-FRONT/rest/GgSearchAction/getInfoMationList',
                        method="POST",
                        headers=self.fake_headers_more_than_49(),
                        formdata=self.fake_data_more_than_49(page),
                        meta={
                            "need_break": False,
                            "page": page,
                            "page_count": page_count,
                        },
                        callback=self.handle_response,
                        dont_filter=True,
                    )


